#include<iostream>
using namespace std;
#include<vector>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include <cstdio>

enum {
  OK=0,
  OPEN_FILE_ERROR,
};
vector<int>data;
int SaveBegin()
{
  string name=to_string(time(nullptr));
  name+=".backup";
  FILE*fp=fopen("name.c_str()","w");
  if(fp==nullptr)return OPEN_FILE_ERROR;
  string dataStr;
  for(auto d:data)
  {
    dataStr+=to_string(d);
    dataStr+=" ";
  }
  fputs(dataStr.c_str(),fp);
  fclose(fp);
  return OK;
}




int Save()
{

  pid_t id =fork();
  if(id==0)
  {
    int code=SaveBegin();
    exit(code);
  }
  int status=0;
  pid_t rid=waitpid(id,&status,0);
  if(rid>0)
  {
    int code=WEXITSTATUS(status);
    if(code==0)printf("备份成功,code :%d",code);
    else printf("备份失败 code:%d",code);

  }

}
int main()
{
  int cnt=1;
  while(true)
  {
    data.push_back(cnt++);
    sleep(1);
    if(cnt%10==0)
    {
      Save();
    }

  }


  return 0;
}
